package cn.itcast.service.impl;

import cn.itcast.dao.BookDao;
import cn.itcast.dao.impl.BookDaoImpl;
import cn.itcast.domain.Book;
import cn.itcast.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.SQLException;

@Service
public class BookServiceImpl implements BookService {
   // @Resource(name = "bookDaoImpl")
    @Autowired
    private BookDao dao;// = new BookDaoImpl();
    @Autowired
    private Connection connection;
    public void save() {
        dao.save();
    }

    @Override
    public void update(int id, double price) {
         dao.update(id,price);
    }

    @Override
    public Book findById(int id) {
        return dao.findById(id);
    }
    public void trans(){
        Book a = dao.findById(1);
        Book b = dao.findById(2);
        update(a.getId(),a.getPrice()-10);
        //int i =1/0;
        update(b.getId(),b.getPrice()+10);
        try {
            connection.commit();
        } catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
